package com.biaoyoo.www.personapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.biaoyoo.www.personapp.PersonApp;
import com.biaoyoo.www.personapp.R;
import com.biaoyoo.www.personapp.utils.LogUtils;

import java.util.ArrayList;

public class SetActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecycleView;
    private ArrayList<String> mDatalist;
    private MyAdatper mAdatper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void loadViews() {
        setContentView(R.layout.activity_set,R.string.set_activity);

        mRecycleView = (RecyclerView) findViewById(R.id.recycle_view);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setOnRefreshListener(this);

    }

    @Override
    protected void initDatas() {
        mDatalist = new ArrayList<>();
        for(int i=0; i<50;i++){
            mDatalist.add("recycleView item is "+i);
        }

        LinearLayoutManager manager = new LinearLayoutManager(SetActivity.this);
        mRecycleView.setLayoutManager(manager);
        mAdatper = new MyAdatper(mDatalist);
        mRecycleView.setAdapter(mAdatper);
        //使用系统自带的itemDecoration，V7包里面的
        mRecycleView.addItemDecoration(new DividerItemDecoration(SetActivity.this,manager.getOrientation()));
        mRecycleView.setItemAnimator(new DefaultItemAnimator()); //使用默认的系统动画
//        mAdatper.notifyItemChanged(position);

        LogUtils.d("size = "+ PersonApp.gArrayList.size());
    }

    @Override
    public void onRefresh() {

        mSwipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(true);
                onImplRefresh();
            }
        });

    }

    private void onImplRefresh() {


        mDatalist.add(0,"999999999999");
        mAdatper.setDatas(mDatalist);
        mSwipeRefreshLayout.setRefreshing(false);
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView mTxtTv;

        public MyViewHolder(View itemView) {
            super(itemView);
            mTxtTv = (TextView) itemView.findViewById(R.id.tv_simeple_list_item);
        }
    }

    class MyAdatper extends  RecyclerView.Adapter<RecyclerView.ViewHolder>{


        ArrayList<String> datalist;
        public MyAdatper(ArrayList<String> datalist) {
            this.datalist = datalist;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_list_item, parent, false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

            ((MyViewHolder)holder).mTxtTv.setText(mDatalist.get(position));
            ((MyViewHolder)holder).mTxtTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = null;
                    if(position == 0){
                        intent = new Intent(SetActivity.this,TabActivity.class);
                    }else if(position == 1){
                        intent = new Intent(SetActivity.this,ScrollingActivity.class);
                    }else if(position == 2){
                        intent = new Intent(SetActivity.this,ButtomNavigationActivity.class);
                    }else{

                    }
                    if(null != intent)
                        startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return mDatalist.size();
        }

        public void setDatas(ArrayList<String> mDatalist) {
            this.datalist = mDatalist;
            notifyDataSetChanged();
        }
    }
}
